package weaver.interfaces.cese.duss.action.fee;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.ReserveUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-31
 * @Description: 备用金移交
 * @ModifyLog:
 **/

public class WeaReserveTransferAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        RecordSet rs = new RecordSet();
        writeLog( "----WeaReserveTransferAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        try {
            String sql = "select byjlx,byjzrr,dzbyjglr,ssfb,ssbm,xmbm,xmmc,bz,yjhkrq,xmwcrq,grbyj,xmbyj from " + tablename + " where requestid=?";
            rs.writeLog( sql, requestid );
            rs.executeQuery( sql, requestid );
            rs.next();
            String byjzrr = Util.null2String( rs.getString( "byjzrr" ) );//备用金责任人
            String grbyj = Util.null2String( rs.getString( "grbyj" ) );//个人已借备用金
            String xmbyj = Util.null2String( rs.getString( "xmbyj" ) );//项目已借备用金
            String dzbyjglr = Util.null2String( rs.getString( "dzbyjglr" ) );//调整备用金管理人
            String ssfb = Util.null2String( rs.getString( "ssfb" ) );//所属分部
            String ssbm = Util.null2String( rs.getString( "ssbm" ) );//所属部门
            String byjlx = Util.null2String( rs.getString( "byjlx" ) );//备用金类型
            String xmbm = Util.null2String( rs.getString( "xmbm" ) );//项目编码
            String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
            String yjhkrq = Util.null2String( rs.getString( "yjhkrq" ) );//预计还款日期
            String bz = Util.null2String( rs.getString( "bz" ) );//说明
            String xmwcrq = Util.null2String( rs.getString( "xmwcrq" ) );//项目完成日期
            String PERSON = "0";//个人
            String PROJECT = "1";//项目
            double yfsjes = 0.00;
            if (PERSON.equals( byjlx )) {
                double yfsje = ReserveUtil.calReserveApplicat( byjlx, dzbyjglr );
                if (ReserveUtil.isExistByj( byjlx, dzbyjglr )) {
                    String sql1 = "update uf_byjglr set yfsje=? where byjlx=? and byjzrr=?";
                    rs.executeUpdate( sql1, new Object[]{yfsje, byjlx, dzbyjglr} );
                    rs.writeLog( sql1, String.join( ",", yfsje + "", byjlx, dzbyjglr ) );
                } else {
                    String sql1 = "insert into uf_byjglr (ssbm,byjlx,byjzrr,ssfb,xmbm,xmmc,yfsje,byjljjkje,hkje,yjhkrq,xmwcrq,bz,byjcs,formmodeid)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    rs.executeUpdate( sql1, new Object[]{ssbm, byjlx, dzbyjglr, ssfb, xmbm, xmmc, grbyj, 0, 0, yjhkrq, xmwcrq, bz, 0, ReserveUtil.getFormmodeid()} );
                    rs.writeLog( sql1, String.join( ",", ssbm, byjlx, dzbyjglr, ssfb, xmbm, xmmc, grbyj, 0 + "", 0 + "", yjhkrq, xmwcrq, bz, 0 + "", ReserveUtil.getFormmodeid() ) );
                }
                yfsjes = ReserveUtil.calReserveBorrow( byjlx, xmbm, byjzrr);
                String sql2 = "update uf_byjglr set yfsje=? where byjlx=? and byjzrr=?";
                rs.executeUpdate( sql2, new Object[]{yfsjes, byjlx, byjzrr} );
                yfsjes = ReserveUtil.calReserveBorrow( byjlx, xmbm, dzbyjglr);
                String sql3 = "update uf_byjglr set yfsje=? where byjlx=? and byjzrr=?";
                rs.executeUpdate( sql3, new Object[]{yfsjes, byjlx, dzbyjglr} );
            }
            if (PROJECT.equals( byjlx )) {
                if (ReserveUtil.isExistByj( byjlx, xmbm )) {
                    //double yfsje = ReserveUtil.calReserveApplicat( byjlx, xmbm );
                   /* String sql2 = "update uf_byjglr set yfsje=?,byjzrr=? where byjlx=? and xmbm=?";
                    rs.executeUpdate( sql2, new Object[]{yfsje, dzbyjglr, byjlx, xmbm} );*/
                    String sql2 = "update uf_byjglr set byjzrr=? where byjlx=? and xmbm=?";
                    rs.executeUpdate( sql2, new Object[]{dzbyjglr, byjlx, xmbm} );
                    rs.writeLog( sql2, String.join( ",",  dzbyjglr, byjlx, xmbm ) );
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
